# conda activate DCTWM 
# conda activate iWM

# https://russianblogs.com/article/71552155624/

# pip install opencv-python
import cv2, os.path
import numpy as np

name = "imgs/MauritsCornelisEscher.jpg" 

y = cv2.imread(name, 0) # Открыто в режиме серого

Y = cv2.dct(y.astype(np.float32))

# print('\nBefore change:')
# for i in range(30):
# 	print(Y[0][i])

# with open(f'dct_coeff_{name[:-4]}.npy', 'wb') as f:
#     np.save(f, Y)

# with open(f'dct_coeff_{name[:-4]}.txt', 'w') as f:
#     for i in range(30):
#     	f.write(str(Y[0][i]) + "\n")


if os.path.exists(f'{name[:-4]}T.jpg') == True:

    name1 = f"{name[:-4]}T.jpg"
    y1 = cv2.imread(name1, 0) # Открыто в режиме серого
    Y1 = cv2.dct(y1.astype(np.float32))

    with open(f'files/DIFF_dct_coeff_{name[5:-4]}.txt', 'w') as f:
        for i in range(30):
            f.write(str(abs(Y[0][i]-Y1[0][i])) + "\n")

    print('\nDIFF_dct_coeff:')
    for i in range(30):
        print(abs(Y[0][i]-Y1[0][i]))





# before = Y[0][0]

# Y[0][0] = 345678

# for i in range(6):
# 	Y[0][i] += 1000

# print('\nAfter change:')

# for i in range(6):
# 	print(Y[0][i])

# cv2.imwrite(f"Dct_{name[:-4]}.jpg", Y)

# y2 = cv2.idct(Y)

# # cv2.imwrite(f"iDCT_{name[:-4]}_{before}_{Y[0][0]}.jpg", y2.astype(np.uint8))

# cv2.imwrite(f"iDCT_{name[:-4]}.jpg", y2.astype(np.uint8))

# Before change:
# 418532.1
# -6893.0405
# -9843.02
# -1954.4313
# -14292.334
# 6438.0005

# After change:
# 419532.1
# -5893.0405
# -8843.02
# -954.4313
# -13292.334
# 7438.0005